package com.example.dao;

import com.example.model.Classes;
import com.example.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ClassDAOImpl implements ClassDAO {

    @Override
    public List<Classes> findAll() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Classes> classes = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM classes";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Classes clazz = new Classes();
                clazz.setClassId(rs.getInt("classId"));
                clazz.setClassName(rs.getString("className"));
                classes.add(clazz);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }
        return classes;
    }

    @Override
    public Classes findById(int classId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Classes clazz = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM classes WHERE classId = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, classId);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                clazz = new Classes();
                clazz.setClassId(rs.getInt("classId"));
                clazz.setClassName(rs.getString("className"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }
        return clazz;
    }

    @Override
    public int insert(Classes clazz) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int result = 0;

        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO classes (classId, className) VALUES (?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, clazz.getClassId());
            pstmt.setString(2, clazz.getClassName());
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, null);
        }
        return result;
    }

    @Override
    public int update(Classes clazz) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int result = 0;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE classes SET className = ? WHERE classId = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, clazz.getClassName());
            pstmt.setInt(2, clazz.getClassId());
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, null);
        }
        return result;
    }

    @Override
    public int delete(int classId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int result = 0;

        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM classes WHERE classId = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, classId);
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, null);
        }
        return result;
    }
}